#ifndef COMMANDPARSE_H
#define COMMANDPARSE_H

#include <QByteArray>
#define BigAndSmall

namespace Command {
enum RecvCmd{
    RecvCameraInfo           = 0x11,
    RecvCameraMode           = 0x12,
    RecvTriggerSource        = 0x13,
    RecvInternalTrigger      = 0x14,
    RecvExpTime              = 0x15,
    RecvFrame                = 0x16,
    RecvAnalogGain           = 0x17,
    RecvHeartBeat            = 0x18,
    RecvOutputWindow         = 0x19,
    RecvTemp                 = 0x1A,
    RecvOutputType           = 0x1B,
    RecvRecover              = 0x1C,
    RecvSaveLocation         = 0x1D,
    RecvSetPhotoMessage      = 0x1E,
    RecvSetPhotoBits         = 0x1F,
    RecvClearStorage         = 0x20,
    RecvExposureCount        = 0x22
};

enum SendCmd{
    CmdExternExposure       = 0xF1,
    CmdCameraInfo           = 0xE1,
    CmdCameraMode           = 0xE2,
    CmdTriggerSource        = 0xE3,
    CmdInternalTrigger      = 0xE4,
    CmdExpTime              = 0xE5,
    CmdFrame                = 0xE6,
    CmdAnalogGain           = 0xE7,
    CmdHeartBeat            = 0xE8,
    CmdOutputWindow         = 0xE9,
    CmdTemp                 = 0xEA,
    CmdOutputType           = 0xEB,
    CmdRecover              = 0xEC,
    CmdSaveLocation         = 0xED,
    CmdSetPhotoMessage      = 0xEE,
    CmdSetPhotoBits         = 0xEF,
    CmdPicture              = 0xDF,
    CmdClearStorage         = 0xD0,
    CmdExposureCount        = 0xD2,
    CmdDeviceError          = 0xC3
};

#pragma pack(push, 1)//设定为1字节对齐
struct CommandHeader
{
    unsigned char preamble[2];		//以0xEB90h 标志一个新的数据包的开始
    unsigned short frameNum;				//帧计数从0000-ffff
    unsigned char cameraId;          //设备码
    unsigned char codeNum;           //指令码
    unsigned int commandLength;      //指令长度
};
#pragma pack(pop)//恢复对齐状态

struct CommandEnd
{
    unsigned char preamble[2];		//以0x0D0Ah 标志一个新的数据包的结尾
};
struct CommandData
{
    CommandHeader header;			//消息头
    QByteArray params;				//具体参数
    unsigned char ExclusiveOrCheck;  //异或校验
    unsigned char SumCheck;         //加和校验
    CommandEnd end;
};

static unsigned char sPreambleSignature[2] = { 0xEB, 0x90};

static unsigned char sEndSignature[2] = { 0x0D, 0x0A};

}

class CommandParse
{
public:
    static QByteArray BuildSendData(const quint16 &frame, const unsigned char &camera_id, const unsigned char &cmd, const QByteArray& data);
    static bool ParseCommands(QByteArray &src_data, QList<QByteArray>& list_data);

    //单条命令最小字节数
    static int ReceiveDataMinBytes();
    static bool ParseOneCommand(QByteArray &src_data, QByteArray& parsed_data);
    //获取命令--命令type
    static unsigned char GetCommandId(const QByteArray &command);
    //检查收发命令是否匹配
    static bool CheckMatchCommand(const QByteArray &cmd1, const QByteArray &cmd2);

private:
    static unsigned char GetRecvCode(unsigned char a);

    static unsigned char sumCheck(QByteArray array);
    static unsigned char xorCheck(QByteArray array);
};

#endif // COMMANDPARSE_H
